<?php
/*
 module:		发布管理
 create_time:	2022-03-09 16:30:59
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\model\ZhChat as ZhChatModel;
use app\api\model\ZhChatLog;
use app\api\model\ZhMatchmakerThankMoney as ZhMatchmakerThankMoneyModel;
use app\api\model\ZhMatchmakerThankNotice;
use app\api\service\ZhMatchmakerThankMoneyService;
use app\api\model\ZhAuthentication as ZhAuthenticationModel;
use app\backend\model\ZhTel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use app\api\model\ZhChat ;
use app\api\model\Member as MemberModel;

class ZhMatchmakerThankMoney extends Common
{


    function index()
    {
        $m_id = intval($this->request->get('m_id'));
        $a_id = intval($this->request->get('a_id'));
        $limit = $this->request->get('limit', 20, 'intval');
        $page = $this->request->get('page', 1, 'intval');
        $where = [];
        if ($a_id > 0) {
            $where['o.a_id'] = $a_id;
        }
        if ($m_id > 0) {
            $where['o.m_id'] = $m_id;
        }
        $field = '*';
        $orderby = 'o.id desc';
        $res = ZhMatchmakerThankMoneyService::indexList(formatWhere($where), $field, $orderby, $limit, $page);
        foreach ($res['list'] as &$v) {
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        }
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }

    /**
     * @api             {get} /ZhAuthentication/view 02、查看详情
     * @apiGroup        ZhAuthentication
     * @apiVersion      1.0.0
     * @apiDescription  查看详情
     * @apiParam (输入参数：) {string}            authentication_id 主键ID
     * @apiHeader {String} Authorization 用户授权token
     * @apiHeaderExample {json} Header-示例:
     * "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"
     * @apiParam (失败返回参数：) {object}        array 返回结果集
     * @apiParam (失败返回参数：) {string}        array.status 返回错误码 201
     * @apiParam (失败返回参数：) {string}        array.msg 返回错误消息
     * @apiParam (成功返回参数：) {string}        array 返回结果集
     * @apiParam (成功返回参数：) {string}        array.status 返回错误码 200
     * @apiParam (成功返回参数：) {string}        array.data 返回数据详情
     * @apiSuccessExample {json} 01 成功示例
     * {"status":"200","data":""}
     * @apiErrorExample {json} 02 失败示例
     * {"status":"201","msg":"没有数据"}
     */
    function view()
    {
        $id=$this->request->get('id');
        $data['id'] =$id;
       // $res                       = checkData(\app\api\model\ZhMatchmakerThankMoney::field("*")->where($data)->find());
        $sql = 'select  o.id,o.price,o.status, o.createtime,gc_zh_matchmaker.phone,
                gc_zh_authentication.real_name a_name,gc_zh_matchmaker.name  m_name 
                from gc_zh_matchmaker_thank_money  as o  
                left join gc_zh_matchmaker  on o.m_id=gc_zh_matchmaker.matchmaker_id
                 left join  gc_zh_authentication on o.a_id=gc_zh_authentication.authentication_id
                where o.id = '.$id.' limit 1';
        $info = Db::connect('mysql')->query($sql)[0];
        $info["createtime"]=date("Y-m-d H:i:s",$info["createtime"]);
        return $this->ajaxReturn($this->successCode, '返回成功', $info ?: '');
    }

    //红娘发起谢媒费
    function add()
    {
        $m_id = $this->request->post('m_id');
        $matchmaker=\app\api\model\ZhMatchmaker::where(["matchmaker_id"=>$m_id])->find();
        if(!$matchmaker){
            return $this->ajaxReturn($this->errorCode, "红娘不存在");
        }

        $price=doubleval($this->request->post('price'));
        if($price<=0){
            return $this->ajaxReturn($this->errorCode, "金额不可小于0");
        }
        $data['m_id'] =$m_id;
        $data['price'] =$price;
        $data['status']            = 0;
        $data['createtime'] = time();
        $thank_id              = ZhMatchmakerThankMoneyModel::insertGetId($data);
        return $this->ajaxReturn($this->successCode, '操作成功', ["thank_id"=>$thank_id]);
    }
    //佳人同意谢媒费
    function update(){
        $id = $this->request->post('id');
//        $a_id = $this->request->post('a_id');
        $status = $this->request->post('status');
        $wxapp_id = $this->request->post('wxapp_id');
        $matchmaker_thank=ZhMatchmakerThankMoneyModel::find($id);
//        $authentication=ZhAuthenticationModel::where("authentication_id",$a_id)->find();
        $matchmaker=\app\api\model\ZhMatchmaker::where("matchmaker_id",$matchmaker_thank["m_id"])->find();
        if(!$matchmaker_thank){
            return $this->ajaxReturn($this->errorCode, "数据不存在，请传正确id");
        }
//        if(!$authentication){
//            return $this->ajaxReturn($this->errorCode, "佳人不存在，请传正确id");
//        }
//        $user = MemberModel::where(['uid' => $authentication["u_id"]])->find();
//        if(!$user){
//            return $this->ajaxReturn($this->errorCode, "佳人用户数据不存在");
//        }
        if(!in_array($status,[1,2])){
            return $this->ajaxReturn($this->errorCode, "status参数不正确");
        }
        $data['a_id'] =0;
        $data['status'] =$status;
        $data['updatetime'] =time();
        $res=ZhMatchmakerThankMoneyModel::where(["id"=>$id])->update($data);
        if($res){
            //发起通知
            $notice["wxapp_id"]=$wxapp_id;
            $notice["u_id"]=0;//发送人id
            $notice['s_id'] =$matchmaker["u_id"];//接收人
            $notice['create_time'] =date("Y-m-d H:i:s");
            $notice['status'] =1;
            $c_id = ZhChat::insertGetId($notice);
            if($c_id){
                if($status==1){
                    $content="佳人/家长已同意您在".date("Y-m-d H:i:s",$matchmaker_thank["createtime"])."发起的“谢媒费”约定";
                }elseif($status==2){
                    $content="佳人/家长已拒绝您在".date("Y-m-d H:i:s",$matchmaker_thank["createtime"])."发起的“谢媒费”约定";
                }
                $log["wxapp_id"]=$wxapp_id;
                $log["chat_id"]=$c_id;
                $log["type"]="text";
                $log["content"]=$content;
                $log["add_time"]=date("Y-m-d H:i:s");
                $log["u_id"]=$matchmaker["u_id"];
                $log["is_read"]=0;
                $log["record_time"]=date("H:i",time());
                $c_log_id=ZhChatLog::insertGetId($log);
            }
            return $this->ajaxReturn($this->successCode, '操作成功');
        }else{
            return $this->ajaxReturn($this->errorCode, '操作失败');
        }
    }


}

